package com.ys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ys.constant.ShopCode;
import com.ys.exception.CastException;
import com.ys.mapper.UserMapper;
import com.ys.pojo.TradeOrder;
import com.ys.pojo.TradeUser;
import com.ys.pojo.TradeUserMoneyLog;
import com.ys.service.UserMoneyLogService;
import com.ys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, TradeUser> implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public TradeUser findUserById(TradeOrder order) {
        QueryWrapper<TradeUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TradeUser::getUserId,order.getUserId());
        return userMapper.selectOne(queryWrapper);
    }

    @Autowired
    private UserMoneyLogService userMoneyLogService;

    @Override
    public Boolean changeUserMoney(TradeUserMoneyLog userMoneyLog) {
        //判断请求参数是否合法
        if (userMoneyLog == null
                || userMoneyLog.getUserId() == null
                || userMoneyLog.getUseMoney() == null
                || userMoneyLog.getOrderId() == null
                || userMoneyLog.getUseMoney().compareTo(BigDecimal.ZERO) <= 0) {
            CastException.cast(ShopCode.SHOP_REQUEST_PARAMETER_VALID);
        }
        //查看订单的付款记录，可能存在多条记录
        Integer count = userMoneyLogService.findMonerLogType(userMoneyLog);

        TradeUser tradeUser = userMapper.selectById(userMoneyLog.getUserId());
        //判断余额操作行为
        //付款操作
        if(userMoneyLog.getMoneyLogType().equals(ShopCode.SHOP_USER_MONEY_PAID.getCode())){
            //订单已经付款，则抛异常
            if (count>0){
                CastException.cast(ShopCode.SHOP_ORDER_PAY_STATUS_IS_PAY);
            }

            UpdateWrapper<TradeUser> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TradeUser::getUserId,userMoneyLog.getUserId())
                    .set(TradeUser::getUserMoney,tradeUser.getUserMoney().subtract(userMoneyLog.getUseMoney()));
            //用户账户扣减余额
            userMapper.update(tradeUser,updateWrapper);
            //退款操作
           if(userMoneyLog.getMoneyLogType().equals(ShopCode.SHOP_USER_MONEY_REFUND.getCode())){
               //如果订单未付款,则不能退款,抛异常
               if (count == 0) {
                   CastException.cast(ShopCode.SHOP_ORDER_PAY_STATUS_NO_PAY);
               }
                //防止多次退款
               if (count > 0) {
                   CastException.cast(ShopCode.SHOP_USER_MONEY_REFUND_ALREADY);
               }
               //用户账户添加余额
               UpdateWrapper<TradeUser> updateWrapper2 = new UpdateWrapper<>();
               updateWrapper.lambda().eq(TradeUser::getUserId,userMoneyLog.getUserId())
                       .set(TradeUser::getUserMoney,tradeUser.getUserMoney().add(userMoneyLog.getUseMoney()));
               //用户账户扣减余额
               userMapper.update(tradeUser,updateWrapper2);
            }

        }
        //记录用户使用余额日志
        userMoneyLog.setCreateTime(new Date());

        userMoneyLogService.save(userMoneyLog);

        return ShopCode.SHOP_SUCCESS.getSuccess();
    }

}
